/**
 * User: diotheos
 * Date: 10.01.11
 * Time: 10:24
 */
/**
 * Changes image of element on event
 */
function runSwitcherAnimation(data, $this, currentObject){
/*    var lastElement = data.length-1; */
    $.each(data,function(i, num) {
        $this.delay(1000, 'imgSwitchers');
        $this.queue('imgSwitchers', function(next) {
            $this.attr('src', num.url);
            /* show something, and if they hit "yes", run the next function. */
            next();
            if(!($this.queue('imgSwitchers').length && currentObject.isActive)){
                runSwitcherAnimation(data, $this, currentObject);
            }
        });
    });
    $this.dequeue('imgSwitchers');
}

$.fn.imageLoader = function(params){
	params = (params != undefined)? params:{};
	var options = {};
    var currentObject = {};
	return this.each(function(){
        $(this).mouseenter(function(){
            var $this = $(this);
            currentObject.firstImage = $(this).attr('src');
            currentObject.isActive = true;
            var autoId = parseInt($(this).closest('a').attr('class').replace('auto-url-',''));
            if(autoId){
                var images = utility.json.loader({
                    'url' : 'ajax/get-auto-photos',
                    'data' : {
                        'auto': autoId
                    }
                });
/*               while(currentObject.isActive){
                    runSwitcherAnimation(images.data, $this);
                }
*/
                runSwitcherAnimation(images.data, $this, currentObject);
            }
            return false;
        });
        $(this).mouseleave(function(){
            var $this = $(this);
            $this.attr('src', currentObject.firstImage);
            currentObject.isActive = false;
            $this.queue('imgSwitchers',[]);
            $this.stop();
            return false;
        });
	});
};